package com.banzhi.shop.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;
import com.banzhi.shop.adapter.AwardAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.Award;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.AndroidUtil;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;
//奖金发放列表
public class AwardHistoryActivity extends BaseActivity {
    private ImageView iv_back;

    private SwipeRefreshLayout swipeRefreshlayout;

    private RecyclerView recyclerview;

    private List<Award> awards = new ArrayList<>();
    private AwardAdapter awardAdapter;

    private int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_award_history);
        Translucent.setTranslucent(this);
        setViews();
        initRV();
        loadData(page);
    }

    private void setViews() {
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        swipeRefreshlayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshlayout);
        swipeRefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
    }

    private void initRV() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerview.setHasFixedSize(true);
        awardAdapter = new AwardAdapter(R.layout.adapter_award, awards);
        awardAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        awardAdapter.isFirstOnly(false);
        awardAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                AndroidUtil.copyText(AwardHistoryActivity.this, ((Award) adapter.getData().get(position)).getLogno());
                ToastUtils.makeToastShort("已粘贴至剪切板！");
            }
        });
        awardAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        }, recyclerview);
        recyclerview.setAdapter(awardAdapter);
    }

    private void refresh() {
        awardAdapter.setEnableLoadMore(false);
        loadData(page = 1);
    }

    private void loadMore() {
        swipeRefreshlayout.setEnabled(false);
        loadData(++page);
    }

    private void loadData(final int page) {
        LoadingDialogUtils.showProgress(this);
        RequestParams params = new RequestParams(URLString.hostUrl + URLString.withdrawHistory);
        params.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        params.addBodyParameter("page", page + "");
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LoadingDialogUtils.dismiss();
                LogUtils.i("Result", result);
                try {
                    JSONObject object = new JSONObject(result);
                    int code = object.getInt("code");
                    if (code == 0) {
                        awards = new Gson().fromJson(object.getJSONArray("data").toString(), new TypeToken<List<Award>>() {
                        }.getType());
                        if (awards == null || awards.size() <= 0) {
                            if (page == 1) ToastUtils.makeToastShort("无数据");
//                            else ToastUtils.makeToastShort("没有更多了");
                            return;
                        }
                        awardAdapter.setNewData(awards);
                        awardAdapter.disableLoadMoreIfNotFullPage();
                    } else {
                        ToastUtils.makeToastShort(object.getString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                LogUtils.e("AddAddressImpl", "---onError----" + ex.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                awardAdapter.loadMoreEnd();
                awardAdapter.setEnableLoadMore(true);
                swipeRefreshlayout.setEnabled(true);
                swipeRefreshlayout.setRefreshing(false);
                LoadingDialogUtils.dismiss();
            }
        });
    }


}
